package com.dhecp.project.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.business.domain.ProductCarefullyChosen;
import com.dhecp.project.business.mapper.ProductCarefullyChosenMapper;
import com.dhecp.project.business.service.ProductCarefullyChosenService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 精品库服务类
 * @author: hlf
 * @create: 2023-03-13 10:05
 */
@Service
public class ProductCarefullyChosenServiceImpl implements ProductCarefullyChosenService {


    @Autowired
    private ProductCarefullyChosenMapper productCarefullyChosenMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    
    /**
     * 查询商品库
     * @param params
     * @return List - ProductCarefullyChosen
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<ProductCarefullyChosen> queryDataList(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句


        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "a1.code asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }

        return productCarefullyChosenMapper.select(params);
    }

    /**
     * 通过guid查询单条商品库
     * @param guid
     * @return 实体 ProductCarefullyChosen
     * @author DH-Xxj
     */
    @Override
    public ProductCarefullyChosen findByCode (String code) {
        return productCarefullyChosenMapper.findByCode(code);
    }
 

    /**
     * 通过guid查询单条商品库
     * @param guid
     * @return 实体 ProductCarefullyChosen
     * @author DH-Xxj
     */
    @Override
    public List<ProductCarefullyChosen> listByCodeMerge (String codeMerge) {
        return productCarefullyChosenMapper.listByCodeMerge(codeMerge);
    }


    /**
     * 新增商品库
     * @param productActiveItems 必要参数：商品库对应实体类（ProductCarefullyChosen）的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult saveData(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        ProductCarefullyChosen chosen = JSON.parseObject(params.get("infoItem").toString(), ProductCarefullyChosen.class);

        if (chosen.getGuid() == null || "".equals(chosen.getGuid())){
            chosen.setGuid(RandomValueUtils.getGUID());
            productCarefullyChosenMapper.insert(chosen);
        }else{
        	
        	ProductCarefullyChosen old = productCarefullyChosenMapper.findByCode(chosen.getCode());
        	if (!"是".equals(old.getEffectStock()) && chosen.getEffectStock().equals("是")) {
            	chosen.setUpdateTime(new Date());
			}
            productCarefullyChosenMapper.update(chosen);
        }

        return AjaxResult.success("操作成功");
    }

	/**
	 * 导入产品信息至产品信息过渡表
	 * @param productBasisItems 必要参数：基础商品库对应实体类（ProductBasis）的json格式字符串
	 * @return MessageModel
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult importBatch(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		
		List<ProductCarefullyChosen> list = JSON.parseArray(params.get("rows").toString(), ProductCarefullyChosen.class);
		if (list == null || list.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}

		for (ProductCarefullyChosen entity : list) {
			if (entity.getCode() == null || entity.getCode().length() == 0) {
				return AjaxResult.error("【商品编码】不能为空");
			}
		}

		System.out.println(JSON.toJSONString(list));

		for (ProductCarefullyChosen entity : list) {
			
			productCarefullyChosenMapper.updateByCode(entity);
		}
		return AjaxResult.success("操作成功");
	}

}